/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.qiujuer.genius.ui.widget;

import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AbsButton;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import net.qiujuer.genius.ui.ResourceTable;

/**
 * This is CheckBoxAndTextField widget
 *
 * @since 2021-05-12
 */
public class CheckBoxAndTextField extends DirectionalLayout {
    private static final int ANIMATOR_DURATION = 10;
    private static final int TRANSPARENT = 0;
    private static final int NON_TRANSPARENT = 1;
    private static final int MOVE_Y_DIS = 100;
    private static final float SCALE_MATRIX = 1.33f;
    private static final float DEFAULT_SCALE_MATRIX = 1f;

    private CheckBox none_a;
    private CheckBox none_b;
    private CheckBox custom_a;
    private CheckBox custom_b;

    private TextField leftTextField;
    private TextField rightTextField;

    private Text leftTextFieldText;
    private Text rightTextFieldText;

    private AnimatorProperty leftTextAnimator;
    private AnimatorProperty rightTextAnimator;

    private boolean isGrowOutFromNothingLeft = false;
    private boolean isGrowOutFromNothingRight = false;

    private TextFieldFocusedListener textFieldFocusedListener;

    /**
     * CheckBoxAndTextField
     *
     * @param context
     */
    public CheckBoxAndTextField(Context context) {
        this(context, null);
    }

    /**
     * CheckBoxAndTextField
     *
     * @param context
     * @param attrSet
     */
    public CheckBoxAndTextField(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    /**
     * CheckBoxAndTextField
     *
     * @param context
     * @param attrSet
     * @param styleName
     */
    public CheckBoxAndTextField(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init();
    }

    private void init() {
        initComponent();
        initListener();
    }

    private void initComponent() {
        Component component = LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_checkbox_textfield, null, false);
        addComponent(component);

        none_a = (CheckBox) findComponentById(ResourceTable.Id_checkbox_none_a);
        none_b = (CheckBox) findComponentById(ResourceTable.Id_checkbox_none_b);
        custom_a = (CheckBox) findComponentById(ResourceTable.Id_checkbox_custom_a);
        custom_b = (CheckBox) findComponentById(ResourceTable.Id_checkbox_custom_b);

        leftTextField = (TextField) findComponentById(ResourceTable.Id_left_text_field);
        rightTextField = (TextField) findComponentById(ResourceTable.Id_right_text_field);

        leftTextFieldText = (Text) findComponentById(ResourceTable.Id_left_text_field_text);
        rightTextFieldText = (Text) findComponentById(ResourceTable.Id_right_text_field_text);

        leftTextAnimator = leftTextFieldText.createAnimatorProperty();
        rightTextAnimator = rightTextFieldText.createAnimatorProperty();

        none_b.setChecked(true);
        custom_a.setChecked(true);
    }

    private void initListener() {
        initCheckBoxListener();
        initTextFieldListener();
    }

    private void initCheckBoxListener() {
        none_a.addCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean isChecked) {
                none_b.setEnabled(isChecked);
            }
        });

        custom_a.addCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(AbsButton absButton, boolean isChecked) {
                custom_b.setEnabled(isChecked);
            }
        });
    }

    private void initTextFieldListener() {
        leftTextField.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (textFieldFocusedListener != null) {
                    textFieldFocusedListener.leftTextFieldFocused(leftTextField.isFocused());
                }
            }
        });

        rightTextField.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (textFieldFocusedListener != null) {
                    textFieldFocusedListener.rightTextFieldFocused(rightTextField.isFocused());
                }
            }
        });

        leftTextField.setFocusChangedListener(new FocusChangedListener() {
            @Override
            public void onFocusChange(Component component, boolean isFocused) {
                if (isFocused) {
                    leftTextField.setFocusedColor(leftTextFieldText);
                } else {
                    leftTextField.setEmptyColor(leftTextFieldText);
                }
            }
        });

        rightTextField.setFocusChangedListener(new FocusChangedListener() {
            @Override
            public void onFocusChange(Component component, boolean isFocused) {
                if (isFocused) {
                    rightTextField.setFocusedColor(rightTextFieldText);
                } else {
                    rightTextField.setEmptyColor(rightTextFieldText);
                }
            }
        });

        leftTextField.addTextObserver(new Text.TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                if (s == null || s.length() == 0) {
                    isGrowOutFromNothingLeft = false;

                    leftTextAnimator.moveFromY(leftTextFieldText.getTop());
                    leftTextAnimator.moveToY(MOVE_Y_DIS);
                    leftTextAnimator.scaleXFrom(DEFAULT_SCALE_MATRIX);
                    leftTextAnimator.scaleYFrom(DEFAULT_SCALE_MATRIX);
                    leftTextAnimator.scaleX(SCALE_MATRIX);
                    leftTextAnimator.scaleY(SCALE_MATRIX);
                    leftTextAnimator.setDuration(ANIMATOR_DURATION).alpha(TRANSPARENT).start();
                } else {
                    if (!isGrowOutFromNothingLeft) {
                        isGrowOutFromNothingLeft = true;

                        leftTextAnimator.moveFromY(MOVE_Y_DIS);
                        leftTextAnimator.moveToY(leftTextFieldText.getTop());
                        leftTextAnimator.scaleXFrom(SCALE_MATRIX);
                        leftTextAnimator.scaleYFrom(SCALE_MATRIX);
                        leftTextAnimator.scaleX(DEFAULT_SCALE_MATRIX);
                        leftTextAnimator.scaleY(DEFAULT_SCALE_MATRIX);
                        leftTextAnimator.setDuration(ANIMATOR_DURATION).alpha(NON_TRANSPARENT).start();
                    }
                }
            }
        });

        rightTextField.addTextObserver(new Text.TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                if (s == null || s.length() == 0) {
                    isGrowOutFromNothingRight = false;

                    rightTextAnimator.moveFromY(rightTextFieldText.getTop());
                    rightTextAnimator.moveToY(rightTextFieldText.getTop() + MOVE_Y_DIS);
                    rightTextAnimator.scaleXFrom(DEFAULT_SCALE_MATRIX);
                    rightTextAnimator.scaleYFrom(DEFAULT_SCALE_MATRIX);
                    rightTextAnimator.scaleX(SCALE_MATRIX);
                    rightTextAnimator.scaleY(SCALE_MATRIX);
                    rightTextAnimator.setDuration(ANIMATOR_DURATION).alpha(TRANSPARENT).start();
                } else {
                    if (!isGrowOutFromNothingRight) {
                        isGrowOutFromNothingRight = true;

                        rightTextAnimator.moveFromY(rightTextFieldText.getTop() + MOVE_Y_DIS);
                        rightTextAnimator.moveToY(rightTextFieldText.getTop());
                        rightTextAnimator.scaleXFrom(SCALE_MATRIX);
                        rightTextAnimator.scaleYFrom(SCALE_MATRIX);
                        rightTextAnimator.scaleX(DEFAULT_SCALE_MATRIX);
                        rightTextAnimator.scaleY(DEFAULT_SCALE_MATRIX);
                        rightTextAnimator.setDuration(ANIMATOR_DURATION).alpha(NON_TRANSPARENT).start();
                    }
                }
            }
        });
    }

    public void setTextFieldFocusedListener(TextFieldFocusedListener textFieldFocusedListener) {
        this.textFieldFocusedListener = textFieldFocusedListener;
    }

    /**
     * TextField是否选中的接口回调
     */
    public interface TextFieldFocusedListener {
        /**
         * leftTextFieldFocused
         *
         * @param isFocused 是否选中
         */
        void leftTextFieldFocused(boolean isFocused);

        /**
         * rightTextFieldFocused
         *
         * @param isFocused 是否选中
         */
        void rightTextFieldFocused(boolean isFocused);
    }
}
